package com.digitalart.futbol.hibernate.daoImpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.digitalart.futbol.hibernate.dao.CantonDao;
import com.digitalart.futbol.hibernate.ejb.Canton;
import com.digitalart.futbol.hibernate.ejb.Local;

@Repository
public class CantonDaoImpl implements CantonDao {

	@Autowired(required=true)
	private SessionFactory sessionFactory;

	@Transactional
	public Canton findById(int id) {
		String queryString= "from Canton canton where canton.id_canton=?";

		Canton result = (Canton) this.sessionFactory.getCurrentSession().createQuery(queryString).setParameter(0, id).uniqueResult();	

		return result;
	}

	@Transactional
	public Canton persistOnMerge(Canton objCanton) {
		Canton result = (Canton)this.sessionFactory.getCurrentSession().merge(objCanton);

		return result;
	}

	@Transactional
	public List<Canton> findByProvincia(int id) {
		String queryString= "from Canton canton where canton.id_provincia=?";

		List<Canton> result = (List) this.sessionFactory.getCurrentSession().createQuery(queryString).setParameter(0, id).list();	

		return result;
	}

	@Transactional
	public List<Canton> findAll() {
		String queryString= "from Canton canton";

		List<Canton> result = (List) this.sessionFactory.getCurrentSession().createQuery(queryString).list();	

		return result;
	}

}
